package com.hyh.baowenlioaworking.controller;

import com.hyh.baowenlioaworking.commom.Constants;
import com.hyh.baowenlioaworking.commom.Page;
import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.model.Department;
import com.hyh.baowenlioaworking.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.controller
 * @ClassName DepartmentController
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  14:07
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/query")
    public R queryAll(){
        List<Department> list = departmentService.queryAll();
        if(list==null){
            return R.error(Constants.CODE_400,"查询失败~");
        }
        return R.success(Constants.CODE_200,list);
    }

    /**
     * 增加  修改
     * @param department
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Department department){
        int i = departmentService.saveDepartment(department);
        if(i==0){
            return R.error(Constants.CODE_600, "操作失败");
        }
        return R.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
//    public R delete(@PathVariable Integer id){
//        int i = departmentService.deleteDepartment(id);
//        if(i==0){
//            return R.error(Constants.CODE_600, "删除失败~");
//        }
//        return R.success();
//    }
    public R<String> deleteDepartment(@PathVariable int id) {
        boolean success = departmentService.deleteDepartmentByIdWithCheck(id);
        if (success) {
            return R.success(Constants.CODE_200,"Department deleted successfully");
        } else {
            return R.error(Constants.CODE_500,"Failed to delete department");
        }
    }

    /**
     * 清空部门表
     * @return
     */
    @PostMapping("/deleteAll")
    public R deleteAll(){
        int i = departmentService.deleteAll();
        if(i==0){
            return R.error(Constants.CODE_600, "清空失败~");
        }
        return R.success();
    }

    /**
     * 分页
     * @param currentPage
     * @param size
     * @return
     */
//    @GetMapping("/page")
//    public R selectByPage( Integer currentPage, Integer size){
//        Page<Department> departmentByPage = departmentService.findDepartmentByPage(currentPage, size);
//        if(departmentByPage==null){
//            return R.error(Constants.CODE_400,"错误~");
//        }
//        return R.success(Constants.CODE_200,departmentByPage);
//    }


}